public class Solution {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {
        }
        ListNode(int val) {
            this.val = val;
        }
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    //删除链表中等于给定值 val 的所有节点。
    public ListNode removeElements(ListNode head, int val) {
        ListNode cur = head;
        if(head == null) {
            return head;
        }
        while(head.val == val) {
            head = head.next;
        }
        while(cur != null) {
            if(cur.next.val == val) {
                //删除节点
                cur.next = cur.next.next;
                continue;
            }
            cur = cur.next;
        }
        return head;
    }

    //翻转链表
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode vir = new ListNode(0,head);
        vir.next = head;
        ListNode cur = head.next;
        head.next = null;
        ListNode curNext = null;
        while(cur != null) {
            curNext = cur.next;
            cur.next = vir.next;
            vir.next = cur;
            cur = curNext;
        }
        return vir.next;
    }
}
